<?php
/*
 * @Author: 李志刚
 * @CopyRight: 2022-2099 李志刚（854378082@qq.com）
 * @Date: 2021-03-16 10:37:01
 * @Description: 友情链接分类
 * @LastEditors: 李志刚
 * @LastEditTime: 2022-04-30 09:25:08
 * @FilePath: /CoinCMF/app/Http/Controllers/Console/Content/LinkTypeController.php
 */

namespace App\Http\Controllers\Console\Content;

use App\Exceptions\FireFlyException;
use App\Http\Controllers\Controller;
use App\Http\Requests\BatchRequest;
use App\Http\Requests\Cms\LinkTypeRequest;
use App\Http\Requests\Cms\LinkTypeUpdateRequest;
use App\Http\Requests\DetailRequest;
use App\Models\Content\Link;
use App\Models\Content\LinkType;
use Illuminate\Http\Request;

class LinkTypeController extends Controller
{
    public function getList(Request $request)
    {
        $key = $request->input('key', '');
        $page = $request->input('page', 1);
        $size = $request->input('size', 10);
        $query = LinkType::where(function ($q) use ($key) {
            if ($key != '') {
                $q->where('name', 'like', '%' . $key . '%');
            }
        })->where('del_flag', 0);
        $count = $query->count();
        $list = $query->limit($size)->offset(($page - 1) * $size)->orderBy('id', 'desc')->get();
        $res = ['list' => $list, 'count' => $count];
        return $this->resJson(200, '成功', $res);
    }
    public function postCreate(LinkTypeRequest $request)
    {
        $data = $request->all();
        $create = ['name' => $data['name']];
        LinkType::create($create);
        return $this->resJson(200, '成功');
    }
    public function getDetail(DetailRequest $request)
    {
        $detail_id = $request->input('detail_id');
        $detail = LinkType::findOrFail($detail_id);
        return $this->resJson(200, '成功', $detail);
    }
    public function postEdit(LinkTypeUpdateRequest $request)
    {
        $data = $request->all();
        $detail_id = $data['detail_id'];
        LinkType::where('id', $detail_id)->update(['name' => $data['name']]);
        return $this->resJson(200, '成功');
    }
    public function postRemove(BatchRequest $request)
    {
        $detail_id = $request->input('detail_id', []);
        if (Link::whereIn('linktype_id', $detail_id)->where('del_flag',0)->count() > 0) {
            throw new FireFlyException(403, '友情链接分类下有内容，请先移除内容');
        }
        LinkType::whereIn('id', $detail_id)->update(['del_flag' => 1]);
        return $this->resJson(200, '完成');
    }
}
